<?php 
	class Videos extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}
		//增加用户游币数
		function increase_click_num($vid){
			$sql = "UPDATE {$this->db->dbprefix}video SET click_day = click_day + 1, real_click_num = real_click_num + 1, base_click = base_click + 1, click_num = click_num + 1 WHERE id=$vid";
			$result = $this->db->query($sql);
		}



		//获取视频列表
		function get_video_list($data = array(), $limit = 0, $offset = 10, $order_by = 'v.click_num DESC'){
			$where = " WHERE v.status = 1 ";
			
			if(!empty($data['uid'])) {
				$where.=" AND v.uid IN (". $data['uid'] .")";
			}
			if(!empty($data['src_type'])) {
				$where.=" AND v.src_type = ".$data['src_type'];
			}
			if(!empty($data['cycle'])) {
				if($data['cycle'] == 1) {
					$where.=" AND v.add_time >= ".strtotime("-1 day");
				}
			}
			$fileds = "v.id, v.uid, v.src_type, v.activity_id, a.title AS activity_titile, v.image_url, v.title, v.description, v.duration, v.click_num, v.orinetation, v.id AS vid, v.video_url, v.comment_count, v.up_count, v.share_count, v.add_time";
			$sql = "SELECT ". $fileds ." FROM " . $this->db->dbprefix('video') ." AS v ";
			$sql .= " LEFT JOIN {$this->db->dbprefix}activities AS a ON(a.id=v.activity_id)";
			$sql .= "$where ORDER BY ". $order_by ." LIMIT $limit, $offset";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}
		
		function get_video_by_where($data = array(), $limit = 0, $offset = 10, $order_by = 'v.click_num DESC'){
			$where = " WHERE v.status = 1 ";

			if(!empty($data['notin_uid'])){
				if(is_array($data['notin_uid'])) $data['notin_uid'] = implode(",", $data['notin_uid']);
				$where .= " AND v.uid NOT IN (". $data['notin_uid'] .")";
			}

			if(!empty($data['notin_vid'])){
				if(is_array($data['notin_vid'])) $data['notin_vid'] = implode(",", $data['notin_vid']);
				$where .= " AND v.id NOT IN (". $data['notin_vid'] .")";
			}
			
			if(!empty($data['src_type'])) {
				$where.=" AND v.src_type = ".$data['src_type'];
			}
			
			if(!empty($data['activity_id'])) {
				$where.=" AND v.activity_id = ".$data['activity_id'];
			}
			
			//暂时隐藏掉后面再开放20160607 add by qzg
			// if(!empty($data['cycle'])) {
			// 	if($data['cycle'] == 1) {
			// 		$where.=" AND v.add_time >= ".strtotime("-2 day");
			// 	}
			// }
			
			$fileds = "v.uid, v.src_type, v.activity_id, a1.title AS activity_titile, v.image_url, v.title, v.description, v.duration, v.click_num, v.orinetation, v.id AS vid, v.video_url, v.comment_count, v.up_count, v.share_count, v.add_time, u.nick, u.head_image_url, u.sex, u.aid, u.verified_anchor, u.province , u.city , a.name, a.logo";
			$sql  = "SELECT ". $fileds ." FROM {$this->db->dbprefix}video AS v ";
			$sql .= " LEFT JOIN {$this->db->dbprefix}user AS u ON(u.id=v.uid) ";
			$sql .= " LEFT JOIN {$this->db->dbprefix}alliance AS a ON(a.id=u.aid) ";
			$sql .= " LEFT JOIN {$this->db->dbprefix}activities AS a1 ON(a1.id=v.activity_id) ";
			$sql .= "$where ORDER BY ". $order_by ." LIMIT $limit, $offset";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}
		
	}